<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);
pc_base::load_app_func('global');

class orders extends admin {
	function __construct() {
		parent::__construct();
		pc_base::load_sys_class('form');
		$this->order = pc_base::load_model('order_info_model');
		$this->back_order = pc_base::load_model('back_order_model');
		$this->goods = pc_base::load_model('order_goods_model');
		$this->db = pc_base::load_model('cart_model');
		$this->db2 = pc_base::load_model('product_model');
		$this->address = pc_base::load_model('consignee_address_model');
		$this->bonus = pc_base::load_model('ushop_bonus_model');
		$this->bonus_type = pc_base::load_model('ushop_bonus_type_model');
		$this->orders_status = array('待确认','待付款','待发贷','无效','退贷');
		$this->siteid = $this->get_siteid();
		$this->categorys = getcache('category_content_'.$this->siteid,'ushop');
	}
	function init(){
		include $this->admin_tpl('sale');
	}
	//订单列表
	public function order_list() {
		$orders_status = $this->orders_status;
		$where = "";
		if(isset($_POST['dosubmit'])) {
			extract($_POST['search']);
			if(!empty($order_number)) {
				$where = $where ? $where . " AND order_sn = '$order_number' " : " order_sn = '$order_number' ";
			}
			if(!empty($start_time)) {
				$start_time = strtotime($start_time);
				$where = $where ? $where . " AND shipping_time >= '$start_time' " : " shipping_time >= '$start_time' ";
			}
			if(!empty($end_time)) {
				$end_time = strtotime($end_time);
				$where = $where ? $where . " AND shipping_time <= '$end_time' " : " shipping_time <= '$end_time' ";
			}
			if(!empty($order)) {
				switch($order) {
					case 0:
						$where = $where ? $where . " AND order_status = '0' " : "  order_status= '0' ";
					break;
					case 1:
						$where = $where ? $where . " AND pay_status = '0' " : " pay_status = '0' ";
					break;
					case 2:
						$where = $where ? $where . " AND shipping_status = '0' " : " shipping_status = '0' ";
					break;
					case 3:
						$where = $where ? $where . " AND order_status = '3' " : " order_status = '3' ";
					break;
					case 4:
						$where = $where ? $where . " AND order_status = '4' " : " order_status = '4' ";
					break;
				}
			}
		}
		$page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
		$infos = $this->order->listinfo($where,$order = "order_id DESC",$page, $pages = '12');

		foreach($infos as $key => $value) {
			$infos[$key]['order_status']=order_status($value['order_status']);
			$infos[$key]['shipping_status']=shipping_status($value['shipping_status']);
			$infos[$key]['pay_status']=pay_status($value['pay_status']);
			
			$address = $this->address->get_one(array('address_id'=>$value['address_id']));
			$infos[$key]['consignee_name'] = $address['consignee'];
		}
		$pages = $this->order->pages;
		include $this->admin_tpl('order_list');
	}
	//订单排序
	public function sale_order() {
		$orders_status = $this->orders_status;
		$where = stripcslashes($_POST['infos']);
		if(isset($_POST['search_order'])) {
			$orderby = explode('&',$_POST['search_order']);
		}else{
			$orderby = array('order_id','DESC');
		}
		$page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
		$infos = $this->order->listinfo($where,$order = "$orderby[0] $orderby[1]",$page, $pages = '12');
		foreach($infos as $key => $value) {
			$infos[$key]['order_status']=order_status($value['order_status']);
			$infos[$key]['shipping_status']=shipping_status($value['shipping_status']);
			$infos[$key]['pay_status']=pay_status($value['pay_status']);
			
			$address = $this->address->get_one(array('address_id'=>$value['address_id']));
			$infos[$key]['consignee_name'] = $address['consignee'];
		}
		$pages = $this->order->pages;
		include $this->admin_tpl('order_list');
	}
	//删除订单 
	public function sale_del() {
		if(!isset($_POST['mid']) || empty($_POST['mid'])) {
			showmessage(L('illegal_parameters'), HTTP_REFERER);
		}else{
			if(is_array($_POST['mid'])) {
				foreach($_POST['mid'] as $order_id) {
					$this->order->delete(array('order_id'=>$order_id));
				}
				showmessage(L('operation_success'),HTTP_REFERER);
			}
			showmessage(L('operation_success'), HTTP_REFERER);
		}
	}
	//查看订单
	public function order_info() {
		$order_id = (int)$_GET['order_id'];
		if($order_id < 0) return false;
		$infos = $this->order->listinfo(array('order_id'=>$order_id));
		foreach($infos as $key => $value) {
			$infos[$key]['order_status']=order_status($value['order_status']);
			$infos[$key]['shipping_status']=shipping_status($value['shipping_status']);
			$infos[$key]['pay_status']=pay_status($value['pay_status']);
			switch($value['pay_style']) {
				case 0:
					$infos[$key]['pay_style'] = '在线支付';
					break;
				case 1:
					$infos[$key]['pay_style'] = '公司转账';
					break;
				case 2:
					$infos[$key]['pay_style'] = '邮局汇款';
					break;	
			}
			$infos[$key]['shipping_name'] = '快递运输';
			//收贷人信息
			$data = $this->address->get_one(array('address_id'=>$value['address_id']));
			$infos[$key]['consignee_name'] = $data['consignee_name'];
			$infos[$key]['address'] = $data['consignee_more_address'];
			$infos[$key]['tel'] = $data['consignee_tel'];
			$infos[$key]['zipcode'] = $data['consignee_zip'];
			$infos[$key]['mobile'] = $data['consignee_pho'];
			$infos[$key]['email'] = $data['consignee_email'];
		}	
		$info = $infos[0];	
		//商品信息
		$goods_info = unserialize($info['goods_id']);
		include $this->admin_tpl('order_info');	
	}
	//订单查询
	public function search_order() {
		$order_status = $this->orders_status;
		$pay_status = array('在线支付','公司转账','邮局汇款');
		$send_status = array('快递运输');
		include $this->admin_tpl('search_order');
	}
	//订单查询处理函数
	public function search_order_info() {
		$orders_status = $this->orders_status;
		if(isset($_POST['dosubmit'])) {
			$where = " 1 ";
			extract($_POST['sear']);
			if($order_sn) {
				$where .= " and order_sn = '$order_sn' ";
			}
			if($username) {
				$userid = get_memberinfo_buyusername($username,'userid');
				$where .= " and user_id = '$userid' ";
			}
			if($start_time) {
				$start_time = strtotime($start_time);
				$where .= " and shipping_time >= '$start_time' ";
			}
			if($end_time) {
				$start_time = strtotime($start_time);
				$where .= " and shipping_time <= '$end_time' ";
			}
			if(!empty($order)) {
				switch($order) {
					case 0:
						$where = $where ? $where . " AND order_status = '0' " : "  order_status= '0' ";
					break;
					case 1:
						$where = $where ? $where . " AND pay_status = '0' " : " pay_status = '0' ";
					break;
					case 2:
						$where = $where ? $where . " AND shipping_status = '0' " : " shipping_status = '0' ";
					break;
					case 3:
						$where = $where ? $where . " AND order_status = '3' " : " order_status = '3' ";
					break;
					case 4:
						$where = $where ? $where . " AND order_status = '4' " : " order_status = '4' ";
					break;
				}
			}
			$page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
			$infos = $this->order->listinfo($where,$order = "order_id DESC",$page, $pages = '12');
			foreach($infos as $key => $value) {
			$infos[$key]['order_status']=order_status($value['order_status']);
			$infos[$key]['shipping_status']=shipping_status($value['shipping_status']);
			$infos[$key]['pay_status']=pay_status($value['pay_status']);
			}
			$pages = $this->order->pages;
			include $this->admin_tpl('order_list');
		}
	}
	//退贷单列表
	public function back_list() {
		$where = "";
		if(isset($_POST['dosubmit'])) {
			extract($_POST['search']);
			if(!empty($order_number)) {
				$where = $where ? $where . " AND order_sn = '$order_number' " : " order_sn = '$order_number' ";
			}
			if(!empty($start_time)) {
				$start_time = strtotime($start_time);
				$where = $where ? $where . " AND back_time >= '$start_time' " : " back_time >= '$start_time' ";
			}
			if(!empty($end_time)) {
				$end_time = strtotime($end_time);
				$where = $where ? $where . " AND back_time <= '$end_time' " : " back_time <= '$end_time' ";
			}
		}
		$page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
		$infos = $this->back_order->listinfo($where,$order = "back_time DESC",$page, $pages = '12');
		foreach($infos as $key => $value) {
			$address = $this->address->get_one(array('address_id'=>$value['address_id']));
			$infos[$key]['consignee_name'] = $address['consignee_name'];
		}
		$pages = $this->back_order->pages;
		include $this->admin_tpl('back_order_list');
	}
	//退贷单排序
	public function back_sale_order() {
		$where = stripcslashes($_POST['infos']);
		if(isset($_POST['search_order'])) {
			$orderby = explode('&',$_POST['search_order']);
		}else{
			$orderby = array('back_id','DESC');
		}
		$page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
		$infos = $this->back_order->listinfo($where,$order = "$orderby[0] $orderby[1]",$page, $pages = '12');
		$pages = $this->back_order->pages;
		include $this->admin_tpl('back_order_list');
	}
	//删除退贷单
	public function back_sale_del() {
		if((!isset($_GET['back_id']) || empty($_GET['back_id'])) && (!isset($_POST['back_id']) || empty($_POST['back_id']))) {
			showmessage(L('illegal_parameters'), HTTP_REFERER);
		} else {
			if(is_array($_POST['back_id'])){
				foreach($_POST['back_id'] as $back_id) {
					$this->back_order->delete(array('back_id'=>$back_id));
				}
				showmessage(L('operation_success'),'?m=ushop&c=orders');
			}else{
				$back_id = intval($_GET['back_id']);
				if($back_id < 1) return false;
				$result = $this->back_order->delete(array('back_id'=>$back_id));
				if($result) {
					showmessage(L('operation_success'),HTTP_REFERER);
				} else {
					showmessage(L("operation_failure"),HTTP_REFERER);
				}
			}
			showmessage(L('operation_success'), HTTP_REFERER);
		}
	}
	//查看退贷订单
	public function look_back_sale() {
		if(isset($_POST['dosubmit']) && !empty($_POST['dosubmit'])) {
			$status = $_POST['status'];	
			$order_id = intval($_POST['order_id']);
			if($order_id < 0) return false;
			$update = $this->back_order->update(array('back_status'=>$status),array('back_id'=>$order_id));
			if($update) {
				showmessage(L('operation_success'),HTTP_REFERER);
			} else {
				showmessage(L("operation_failure"),HTTP_REFERER);
			}
		}else{
			$back_id = (int)$_GET['back_id'];
			if($back_id < 0) return false;
			$info = $this->back_order->get_one(array('back_id'=>$back_id));
			$info['user_name'] = get_memberinfo($info['user_id'],'nickname');
			//收贷人信息
			$addressinfo = $this->address->get_one(array('address_id'=>$info['address_id']));
			
			$goods_info = unserialize($info['goods_id']);
			foreach($goods_info as $k => $v) {
				$catid = $v['catid'] = intval($v['catid']);
				$category = $this->categorys[$catid];
				$modelid = $category['modelid'];
				$this->db2->set_model($modelid);
				if($this->db2->table_name==$this->db2->db_tablepre) showmessage(L('model_table_not_exists'));;
				$goods_data[] = $this->db2->get_one(array('id'=>$v['id']));
			}
			include $this->admin_tpl('back_order_info');
		}
	}
}
?>